package com.ruanda.spongebobrunner.utils;

import android.content.Context;
import android.media.MediaPlayer;
import com.ruanda.spongebobrunner.R;
import lombok.Data;

@Data
public class SoundUtils {
    private MediaPlayer soundClick; // 点击
    private MediaPlayer soundHurt; // 受伤
    private MediaPlayer soundJump; // 跳跃
    private MediaPlayer soundCure; // 回复

    public SoundUtils(Context context) {
        try {
            // 加载音频资源
            this.soundClick = MediaPlayer.create(context, R.raw.click);
            this.soundHurt = MediaPlayer.create(context, R.raw.hurt);
            this.soundJump = MediaPlayer.create(context, R.raw.jump);
            this.soundCure = MediaPlayer.create(context, R.raw.cure);
            // 音量调大
            this.soundClick.setVolume(3.0f, 3.0f);
            this.soundJump.setVolume(0.5f, 0.5f);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            // 处理内存不足错误
            e.printStackTrace();
        }
    }

    /**
     * 播放点击音效
     */
    public void playSoundClick() {
        if (soundClick != null) {
            soundClick.seekTo(0);
            soundClick.start();
        }
    }

    /**
     * 播放跳跃音效
     */
    public void playSoundJump() {
        if (soundJump != null) {
            soundJump.seekTo(0);
            soundJump.start();
        }
    }

    /**
     * 播放受伤音效
     */
    public void playSoundHurt() {
        if (soundHurt != null) {
            soundHurt.seekTo(0);
            soundHurt.start();
        }
    }

    /**
     * 播放回复音效
     */
    public void playSoundCure() {
        if (soundCure != null) {
            soundCure.seekTo(0);
            soundCure.start();
        }
    }
}
